package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.AdSensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.wemedia.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.AdSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class AdSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements AdSensitiveService {
    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;
    /**
     * 敏感词查询列表
     *
     * @param sensitiveDto
     * @return
     */
    @Override
    public ResponseResult pageQuery(AdSensitiveDto sensitiveDto) {
        //1. 参数校验
        if (sensitiveDto == null)
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        sensitiveDto.checkParam();
        //2. 组装条件wrapper
        LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.<WmSensitive>lambdaQuery();
        if (StringUtils.isNotEmpty(sensitiveDto.getName())){
            wrapper.like(WmSensitive::getSensitives,sensitiveDto.getName());
        }
        //3. 按创建时间倒序排列
        wrapper.orderByDesc(WmSensitive::getCreatedTime);
        //4. 构建分页page
        Page<WmSensitive> page = new Page<>(sensitiveDto.getPage(),sensitiveDto.getSize());
        //5. 进行分页条件查询
        page(page,wrapper);
        //6. 封装结果返回
        PageResponseResult pr = new PageResponseResult((int)page.getCurrent(),(int)page.getSize(),(int)page.getTotal());
        pr.setData(page.getRecords());
        return pr;
    }

    /**
     * 敏感词新增
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        if (wmSensitive == null || StringUtils.isEmpty(wmSensitive.getSensitives()))
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        //判断新增的敏感词是否已存在
        LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
        WmSensitive sensitive = wmSensitiveMapper.selectOne(wrapper);
        if (sensitive != null)
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 敏感词修改
     *
     * @param sensitiveDto
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitiveDto sensitiveDto) {
        if (sensitiveDto == null)
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(sensitiveDto,wmSensitive);
        //如果修改的敏感词不等于当前id的敏感词
        //判断修改的敏感词是否已存在
        WmSensitive selectById = wmSensitiveMapper.selectById(sensitiveDto.getId());
        if (!selectById.getSensitives().equals(sensitiveDto.getSensitives())) {
            LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
            WmSensitive sensitive = wmSensitiveMapper.selectOne(wrapper);
            if (sensitive != null || StringUtils.isNotEmpty(sensitive.getSensitives()))
                throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        }
        wmSensitiveMapper.updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 敏感词删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        if (id == null)
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        wmSensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}